package com.lovo.invoicing.controller;

import com.lovo.invoicing.bean.SaleCartBean;
import com.lovo.invoicing.bean.UserBean;
import com.lovo.invoicing.bean.vo.OutGoodsBean;
import com.lovo.invoicing.service.ISaleCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/saleCart")
public class SaleCartController {
    @Autowired
    ISaleCartService saleCartService;
    @RequestMapping("/addIntoCart")
    public String addIntoCart(@RequestBody OutGoodsBean outGoodsBean, HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        int a =  saleCartService.addIntoCart(outGoodsBean,u.getUserId());
        if (a==0){
            return "error";
        }
        return "success";
    }

    @RequestMapping("/removeSaleCart")
    public String removeSaleCart(int saleCartId){
        saleCartService.deleteSaleCartById(saleCartId);
        return "success";
    }

    @RequestMapping("/findSaleCarts")
    public List<SaleCartBean> findSaleCarts(HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        return saleCartService.querySaleCarts(u.getUserId());
    }

    @RequestMapping("/modifySaleCart")
    public int modifySaleCart(@RequestBody SaleCartBean saleCartBean){
        return saleCartService.updateSaleCart(saleCartBean);
    }
}
